package com.zxbg.service.impl.systemtoolsServiceImpl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zxbg.dao.InfoBlockDao;
import com.zxbg.dao.InfoBlockTypeDao;
import com.zxbg.domain.InfoBlock;
import com.zxbg.domain.InfoBlockType;
import com.zxbg.service.systemtoolsService.InfoBlockTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InfoBlockTypeServiceImpl implements InfoBlockTypeService {
    @Autowired
    InfoBlockTypeDao infoBlockTypeDao;
    @Autowired
    InfoBlockDao infoBlockDao;

    @Override
    public boolean updateBlockType(List<InfoBlockType> newList, String belong_block_id, Integer tenant_id) {
        List<InfoBlockType> oldList = getBlockTypeByBlockId(belong_block_id, tenant_id);
        try {
            for (int i = 0; i < oldList.size(); i++) {
                InfoBlockType oldTemp = oldList.get(i);
                int j = 0;
                for (j = 0; j < newList.size(); j++) {
                    String jsonObject = JSON.toJSONString(newList.get(j));
                    InfoBlockType newTemp = JSONObject.parseObject(jsonObject, InfoBlockType.class);
                    if(oldTemp.getType_id() == newTemp.getType_id()) {
                        if(oldTemp.getType_name() != newTemp.getType_name()) {
                            if(newTemp.getType_name().length() == 0) {
                                changeDeleteIdToOther(belong_block_id, tenant_id, newTemp.getType_id());
                                infoBlockTypeDao.deleteInfoBlockTypeByTypeId(newTemp.getType_id());
                            } else {
                                infoBlockTypeDao.updateTypeNameByTypeId(newTemp.getType_name(), newTemp.getType_id());
                            }
                        }
                        break;
                    }
                }
                if(j == newList.size() && !oldTemp.getType_name().equals("其他")) {
                    changeDeleteIdToOther(belong_block_id, tenant_id, oldTemp.getType_id());
                    infoBlockTypeDao.deleteInfoBlockTypeByTypeId(oldTemp.getType_id());
                }
            }
            for (int i = 0; i < newList.size(); i++) {
                String jsonObject = JSON.toJSONString(newList.get(i));
                InfoBlockType temp = JSONObject.parseObject(jsonObject, InfoBlockType.class);
                if(temp.getTenant_id() == null) {
                    if(temp.getType_name().length() != 0) {
                        temp.setTenant_id(tenant_id);
                        infoBlockTypeDao.addInfoBlockType(temp);
                    }
                }
            }
            return true;
        } catch (Exception ex) {
            System.out.println(ex);
            return false;
        }
    }

    public void changeDeleteIdToOther(String belong_block_id, Integer tenant_id, Integer type_id) {
        Integer newId = -1;
        List<InfoBlockType> infoBlockTypeList = getBlockTypeByBlockId(belong_block_id, tenant_id);
        for (int i = 0; i < infoBlockTypeList.size(); i++) {
            InfoBlockType temp = infoBlockTypeList.get(i);
            if(temp.getType_name().equals("其他")) {
                newId = temp.getType_id();
                break;
            }
        }
        List<InfoBlock> infoBlockList = infoBlockDao.getListByTypeId(type_id);
        for (int i = 0; i < infoBlockList.size(); i++) {
            infoBlockDao.updateBlockTypeId(infoBlockList.get(i).getId(), newId);
        }
    }

    @Override
    public List<InfoBlockType> getBlockTypeByBlockId(String belong_block_id, Integer tenant_id) {
        return infoBlockTypeDao.getBlockTypeByBlockId(belong_block_id, tenant_id);
    }
}
